# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: oo_te_alignment.py
  @time: 2022/3/13 14:01
  @desc:稳偏的脚本
"""
import time
from ATS.hardwarescan import conf_file_manager
from ATS.application.LASER_App.N7744C_App import N7744C_App
from ATS.application.LASER_App.L8164B_App import L8164B_App
from ATS.application.LASER_App.N7786C_App import N7786C_App
from ATS.util.calcu_step_util import get_step_list
from ATS.switchdriver.LightSwitchDriver import LightSwitchDriver


def oo_te_alignment(wave_length=1310, power_value=0, max_min=1, n7744_power_range=0):
    n7744c_app = N7744C_App("SS2")
    l8164b_app = L8164B_App("SS3")
    n7786c_app = N7786C_App("SS1")
    #
    # inst_light = LightSwitchDriver.instance()
    # if inst_light.is_connected:
    #     inst_light.set_all_switch("01", "05")
    #     time.sleep(1)
    #     print(inst_light.query_switch())
    # else:
    #     inst_light.connect()
    # max_min: 0代表显示最小,1代表显示最大
    global polarization_data
    # n7744c_app.set_logging_state("STOP")
    # l8164b_app.cls()
    l8164b_app.set_wave_length(wave_length)
    l8164b_app.set_power_unit()
    l8164b_app.set_power_value(power_value)
    time.sleep(4)
    l8164b_app.output_on()
    print("after 8164")
    n7744c_app.reset()
    n7744c_app.set_power_range(n7744_power_range)
    n7744c_app.set_auto_gain()
    n7744c_app.trigger_input_mode()
    n7744c_app.set_power_wave_length(wave_length)
    n7744c_app.set_logging_points()
    print("after 7744")
    n7786c_app.reset()
    n7786c_app.set_stabilization_mode(0)
    n7786c_app.set_random_points_stepwidth()
    n7786c_app.set_trigger_conf_mode(4)
    n7786c_app.set_sample_point_rate()
    n7786c_app.set_wave_length(wave_length)
    n7786c_app.start_polarimeter_logging()
    n7786c_app.start_polarization_sequence()
    print("after 7786")
    n7744c_app.set_logging_state("START")
    n7786c_app.trigger()
    n7744c_app.tigger_status()
    while True:
        state = n7744c_app.get_func_state()
        if state == "LOGGING_STABILITY,COMPLETE":
            result_list = n7744c_app.get_func_res()
            break
    max_index = result_list.index(max(result_list))
    min_index = result_list.index(min(result_list))
    while True:
        log_value = n7786c_app.get_logged_value()
        if log_value == "+0":
            polarization_data = n7786c_app.get_polarization_sweep_data()
            break
    n7786c_app.stop_logging_polcontroller()
    state = n7786c_app.get_opc_state()
    if state == "1":
        n7786c_app.set_external_trigger_delay()
        n7786c_app.set_trigger_conf_mode()
        n7786c_app.set_stabilization_mode(1)

    max_polar = [polarization_data[max_index * 3], polarization_data[max_index * 3 + 1],
                 polarization_data[max_index * 3 + 2]]
    min_polar = [polarization_data[min_index * 3], polarization_data[min_index * 3 + 1],
                 polarization_data[min_index * 3 + 2]]
    print(max_polar)
    if max_min:
        n7786c_app.set_polarization_value(*max_polar)
    else:
        n7786c_app.set_polarization_value(*min_polar)


oo_te_alignment()
